var userService = require("../service/user")
const map = new Map()

module.exports = function (app) {

    app.ws("/w", function (ws, req) {
        console.log("鏈接服務器成功")
        ws.ids = parseInt(req.query.id)
        map.set(parseInt(req.query.id), ws)

        ws.on('message', function (msg) {
            let body
            try {
                body = JSON.parse(msg)
            } catch (error) {
                console.log(error)
            }
            let type = body.type //标识发送给个人还是发送给群
            console.log('标识发送给个人还是发送给群')
            switch (type) {
                case 'user': {
                    body.state = 1
                    if (map.get(body.peerId) != null) {
                        body.state = 0
                        console.log(`收到${body.userId}发来发送给${body.peerId}的消息`)
                        map.get(body.peerId).send(JSON.stringify(body))
                    }
                    userService.handleUserMsg(body)
                    break
                }
                case 'isOnline': {
                    if (map.get(body.peerId) != null) {
                        ws.peerId = body.peerId
                        map.get(body.peerId).peerId = body.userId
                        ws.send(JSON.stringify({
                            type: "isOnline",
                            isOnline: true,
                            id: body.userId,
                        }))
                        map.get(body.peerId).send(JSON.stringify({
                            type: "isOnline",
                            isOnline: true,
                            id: body.userId
                        }))
                        ws.send(JSON.stringify({
                            type: "start",
                            userId: body.userId,
                            peerId: body.peerId
                        }))

                    } else {
                        ws.send(JSON.stringify({type: "isOnline", isOnline: false}))
                    }
                    break
                }
                case "offer": {
                    let peerCon = map.get(body.to)
                    if (peerCon != null) {
                        peerCon.send(JSON.stringify({
                            type: "offer",
                            offer: body.offer,
                            from: body.from,
                            to: body.to,
                            flag: "remote"
                        }))
                    }
                    break
                }
                case "answer": {
                    console.log("answer")
                    let peerCon = map.get(body.to)
                    if (peerCon != null) {
                        peerCon.send(JSON.stringify({
                            type: "answer",
                            answer: body.answer,
                            from: body.from,
                            to: body.to
                        }))
                    }
                    break
                }
                case "candidate": {
                    let peerCon = map.get(body.to)
                    if (peerCon != null) {
                        peerCon.send(JSON.stringify({
                            type: "candidate",
                            candidate: body.candidate,
                            from: body.from,
                            to: body.to
                        }))
                    }
                    break
                }
                case 'group': {
                    break
                }
                default: {
                    // ws.close()
                }
            }
        })

        ws.on("close", function (e) {
            console.log("ws is closed")
        })
    })
}
